//
//  Director.m
//  HappyPenguin
//
//  Created by zhuangyihang on 12/30/15.
//  Copyright © 2015 zhuangyihang. All rights reserved.
//

#import "Director.h"

#import "HttpClient.h"
#import "AppConstants.h"
#import "DDFormatter.h"

#import "ObjectBuilder.h"
#import "HUD.h"

#import "HPSecondTabViewController.h"
#import "HPToDoMainViewController.h"
#import "REFrostedViewController.h"
#import "HPLeftMenuViewController.h"

#import <KVNProgress/KVNProgress.h>

static NSString *CONFIG_USER_KEY = @"CONFIG_USER_KEY";

@interface Director()<REFrostedViewControllerDelegate>

@property (nonatomic, strong) UserObject *userObject;

@end

@implementation Director

+ (id)sharedInstance
{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init]; // or some other init method
    });
    return _sharedObject;
}

- (id)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)setupApp{
    //设置api地址
    [[HttpClient client] setHttpBaseUrl:[AppConstants apiUrl]];
    
    [self setupTool];
    
    [[HUD hud] setupHud];
    
    [self getLoginUser];
}

- (void)setupTool{
    DDFormatter *formatter = [[DDFormatter alloc] init];
    [[DDASLLogger sharedInstance] setLogFormatter:formatter];
    [DDLog addLogger:[DDASLLogger sharedInstance]];
    [[DDTTYLogger sharedInstance] setLogFormatter:formatter];
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
}

- (UIViewController *)getTabViewController{
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    HPToDoMainViewController *todo = [[HPToDoMainViewController alloc] init];
    HPSecondTabViewController *cal = [[HPSecondTabViewController alloc] init];
    tab.viewControllers = @[cal,todo];
    tab.tabBar.tintColor = [UIConstants getAppColor];
    
    HPLeftMenuViewController *menu = [[HPLeftMenuViewController alloc] init];
    
    REFrostedViewController *frostedViewController = [[REFrostedViewController alloc] initWithContentViewController:tab
                                                                                                 menuViewController:menu];
    frostedViewController.direction = REFrostedViewControllerDirectionLeft;
    frostedViewController.liveBlurBackgroundStyle = REFrostedViewControllerLiveBackgroundStyleLight;
    frostedViewController.liveBlur = YES;
    frostedViewController.delegate = self;
    return frostedViewController;
}

- (UserObject *)getLoginUser{
    if (self.userObject) {
        return self.userObject;
    }
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:CONFIG_USER_KEY];
    if (dic) {
        self.userObject = [[ObjectBuilder builder]objectFromJSON:dic className:@"UserObject"];
    }else{
        return [UserObject new];
    }
    
    return self.userObject;
}

- (void)setLoginUser:(UserObject *)user save:(BOOL)save{
    if (user==nil) {
        return;
    }
    self.userObject = user;
    if (save) {
        NSDictionary *dic = [[ObjectBuilder builder] JSONDictionaryFromObject:user];
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:CONFIG_USER_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

#pragma mark - REFrostedViewControllerDelegate
@end
